package com.game77.guessTheWords2.sprite;

import java.text.DecimalFormat;

import android.graphics.Typeface;

import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.entity.StageData;
import com.game77.guessTheWords2.layer.GameLayer;
import com.game77.guessTheWords2.scene.GameScene;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.nodes.AtlasLabel;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;

/**
 * 
 * @author Administrator
 * 
 */
public class StageBlock extends Node {

	Button button;
	AtlasLabel stageLabel;
	Sprite background;
	Sprite background_locked;
	StarRate rate;
	Label complateInfo;
	Label useTime;
	Label score;

	int stageIndex;
	static DecimalFormat secondFormat = new DecimalFormat("00");

	public StageBlock(int index) {
		background_locked = Sprite.make(GameSystem.tex_stage_block_locked);
		background_locked.setAnchorX(0);
		background_locked.setPosition(0, 0);
		background_locked.setVisible(false);
		addChild(background_locked);

		background = Sprite.make(GameSystem.tex_stage_block);
		background.setAnchorX(0);
		background.setPosition(0, 0);
		addChild(background);

		stageLabel = AtlasLabel.make(index + "", GameSystem.tex_numbers_48,
				GameSystem.charMap);
		stageLabel.setAnchorX(0.5f);
		stageLabel.setPosition(45, 3);
		stageLabel.setColor(WYColor3B.make(255, 255, 255));
		stageLabel.setScaleX(0.8f);
		addChild(stageLabel);

		stageIndex = index;
		button = Button.make(R.drawable.button_go_normal,
				R.drawable.button_go_pressed, this, "onClick");
		button.setPosition(371, 3);
		addChild(button);

		complateInfo = Label.make("0/24", 20);
		complateInfo.setColor(WYColor3B.make(255, 255, 255));
		complateInfo.setPosition(178, -18);
		addChild(complateInfo);

		useTime = Label.make("0'00", 20);
		useTime.setColor(WYColor3B.make(255, 255, 255));
		useTime.setAnchorX(0);
		useTime.setPosition(276, -18);
		addChild(useTime);

		rate = new StarRate();
		rate.setPosition(145, 23);
		addChild(rate);

		score = Label.make(0 + "", 23, Typeface.BOLD);
		score.setColor(WYColor3B.make(200, 150, 28));
		score.setAnchorX(0);
		score.setPosition(215, 22);
		addChild(score);

		refreshStageInfo();
	}

	public void onClick() {
		AudioHelper.clickButton();
		GameUtil.switchSceneFlipX(GameScene.make());
		GameLayer.instance.startGame(stageIndex);

		// 显示广告
		ADHelper.showAd();
	}

	public void refreshStageInfo() {
		String stageKey = StageManager.instance.getLastClass() + "_"
				+ stageIndex;
		StageData sd = StageManager.instance.getStageDatas().get(stageKey);
		if (sd != null) {
			rate.setRate(sd.getComplateCount() / (float) sd.getAllCount());
			complateInfo
					.setText(sd.getComplateCount() + "/" + sd.getAllCount());

			score.setText(sd.getScore() + "");

			useTime.setText(getUseTimeStr(sd.getTotalSeconds()));
		}
	}

	private String getUseTimeStr(long totalSeconds) {
		return (totalSeconds / 60) + "'"
				+ secondFormat.format(totalSeconds % 60);
	}

	/**
	 * 设置锁定状态
	 * 
	 * @param lock
	 */
	public void setLockStatus(boolean lock) {
		if (lock) {
			background.setVisible(false);
			button.setVisible(false);
			background_locked.setVisible(true);
			rate.setVisible(false);
			complateInfo.setVisible(false);
			useTime.setVisible(false);
			score.setVisible(false);
		} else {
			background.setVisible(true);
			button.setVisible(true);
			background_locked.setVisible(false);
			rate.setVisible(true);
			complateInfo.setVisible(true);
			useTime.setVisible(true);
			score.setVisible(true);
		}
	}
}
